<head th:replace="common :: common_head(~{},~{})" xmlns:th="http://www.thymeleaf.org"
      xmlns="http://www.w3.org/1999/html">
</head>

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <form id="allInputsFormValidation" class="form-horizontal"
                  novalidate="">
                <div class="content">
                    <legend>用户个人信息</legend>
                    <input type="hidden" name="id" value=""/>
                    <input type="hidden" name="state" value=""/>
                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户名称
                                <star>*</star>
                            </label>
                            <div class="col-sm-6">
                                <input class="form-control" type="text" name="name" id="name"
                                />
                            </div>
                        </div>
                    </fieldset>

                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">手机号
                                <star>*</star>
                            </label>
                            <div class="col-sm-6">
                                <input class="form-control" type="text" name="phone" id="phone"
                                       required="required"/>
                            </div>
                        </div>
                    </fieldset>

                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">密码
                                <star>*</star>
                            </label>
                            <div class="col-sm-3">
                                <input class="form-control" name="password"
                                       id="registerPassword" type="text" readonly="readonly"/>
                            </div>
                            <div class="col-sm-3">
                                <button class="form-control btn btn-info btn-outline" type="button"
                                        onclick="window.location.href = '/user/findByIdToPassword?id=' + $('input[name=id]').val()">
                                    修改密码
                                </button>
                            </div>
                        </div>
                    </fieldset>

                    <fieldset>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">用户角色
                                <star>*</star>
                            </label>
                            <div class="col-sm-6">
                                <select class="form-control" id="myselect" required="required"
                                        onfocus="this.defaultIndex=this.selectedIndex;"
                                        onchange="this.selectedIndex=this.defaultIndex;" name="roleId"
                                        readonly="readonly">
                                </select>
                            </div>
                            <!--<div class="col-sm-4"><code>url="true"</code></div>-->
                        </div>
                    </fieldset>


                </div>
                <div class="footer text-center">
                    <button type="submit" class="btn btn-info btn-fill">修改</button>
                    <!--					<button type="reset" class="btn btn-info btn-fill">重置</button>-->

                </div>
            </form>

        </div>
        <!--  end card  -->
    </div>
    <!-- end col-md-12 -->
</div>
<!-- end row -->


<script type="text/javascript">


    $().ready(function () {
        //查询个人信息页面
        $.ajax({
            dataType: "json",
            url: "/user/userInfo",
            type: "post",
            success: function (data) {
                $("input[name='id']").val(data.id);
                $("input[name='state']").val(data.state);
                $("input[name='name']").val(data.name);
                $("input[name=phone]").val(data.phone);
                $("input[name=password]").val("******");
                if (data.roleid == 1) {
                    $("<option value='1' >管理员</option>").appendTo("#myselect");//构造html元素并插入
                } else if (data.roleid == 2) {
                    $("<option value='2'>库存管理人员</option>").appendTo("#myselect");//构造html元素并插入
                } else if (data.roleid == 3) {
                    $("<option value='3' >业务人员</option>").appendTo("#myselect");//构造html元素并插入
                } else {
                    $("<option value='4'>普通用户</option>").appendTo("#myselect");//构造html元素并插入
                }
            }
        });

        //重写验证规则，设置返回中文
        $.validator.messages.required = jQuery.validator.format("请填写必填字段");
        // 手机号码验证
        jQuery.validator.addMethod("isMobile", function (value, element) {
            var length = value.length;
            return (length == 11 && /^((1[0-9]{1})+\d{9})$/.test(value));
        }, "请正确填写您的手机号码。");


        $('#allInputsFormValidation').validate({
            submitHandler: function (form) { //通过之后回调
                //进行ajax传值
                $.ajax({
                    data: $("#allInputsFormValidation").serialize(),
                    dataType: "text",
                    type: "post",
                    url: "/user/userupdate",
                    success: function (data) {
                        console.log(data);
                        if (data == "1") {
                            window.location.href = "/page/user_info";
                        } else {
                            swal("糟糕！", data, "error");
                        }
                    },
                    error: function () {
                        swal("出错了！", "服务器通信失败，请检查网络状态", "error");
                    }
                });
            },
        });


    });


</script>

<script>


</script>

</html>
